package com.neusoft.neuiotms.dcam.common.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcam.common.converter.PrinterConverter;
import com.neusoft.neuiotms.dcam.common.dto.PrinterDTO;
import com.neusoft.neuiotms.dcam.common.model.Printer;
import com.neusoft.neuiotms.dcam.common.repository.PrinterRepository;
import com.neusoft.neuiotms.dcam.common.service.PrinterService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "打印机管理")
@RestController
@RequestMapping(value = "/api/w/printers")
public class PrinterController {

    @Autowired
    private PrinterService printerService;
    @Autowired
    private PrinterRepository printerRepository;
    @Autowired
    private PrinterConverter printerConverter;

    @GetMapping("/{belongs}/list")
    public ResultListDTO<?> getConfig(@PathVariable final String belongs) {
        final List<PrinterDTO> dto = this.printerService.getPrinterConfig(belongs);
        return ResultListDTO.success(dto);
    }

    @GetMapping("/me")
    public ResultDTO<?> getMyConfig(final UserBean userBean) {
        final List<String> myOrgIn = userBean.getOrgs();
        PrinterDTO dto = null;
        for (final String org : myOrgIn) {
            dto = this.printerService.getMyPrinter(org);
            if (null != dto) {
                break;
            }
        }
        if (null == dto) {
            return ResultDTO.success();
        }
        return ResultDTO.success(dto);
    }

    @PostMapping
    public ResultDTO<PrinterDTO> create(@RequestBody final PrinterDTO dto) {
        final PrinterDTO result = this.printerService.setPrinterConfig(dto);
        return ResultDTO.success(result);
    }

    @GetMapping("/{id}")
    public ResultDTO<PrinterDTO> update(@PathVariable final Long id) {
        final Optional<Printer> opt = this.printerRepository.findById(id);
        if (!opt.isPresent()) {
            throw new UnitedException("打印机不存在");
        }
        return ResultDTO.success(this.printerConverter.toDTO(opt.get()));
    }

    @PutMapping("/{id}")
    public ResultDTO<PrinterDTO> update(@PathVariable final Long id,
            @RequestBody final PrinterDTO dto) {
        dto.setId(id);
        final PrinterDTO result = this.printerService.setPrinterConfig(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.printerService.delete(id);
        return ResultDTO.success();
    }
}
